#!usr/bin/python
# -*- coding:utf-8 -*-
#Copyright 2020 labsight
import sys
import os
from base.func.email import Email
from base.pyPlugin import PyPlugin
from base.pyLog import PyLog

class PySett():
    def __init__(self,workpath,argv):
        self.workpath = workpath
        self.argv = argv
        self.log = self._init_log(self.workpath, self.argv)

    def _init_log(self,workpath, argv):
        loglevel = PyLog.DEBUG if "-debug" in argv else PyLog.INFO
        if os.path.exists(workpath):
            logpath = os.path.join(workpath, "log")
            if not os.path.exists(logpath):
                os.mkdir(logpath)
            logpath = os.path.join(logpath, '%s.log' % argv[0])
        else:
            logpath = None
        if '-skiperr' in argv:
            PyLog.logexit(False)
        log = PyLog(logname='agt').loginit(loglevel=loglevel, logpath=logpath)
        lenline, title = 80, "Pysail"
        log.info("=" * lenline)
        log.info("%s%s" % (" " * int((lenline - len(title)) / 2), title))
        log.info("=" * lenline)
        log.info("workpath路径地址：%s" % workpath)
        log.info("args系统参数：%s" % " ".join(sys.argv[1:]))
        return log

    def get_argv_value(self,argv_key):
        if argv_key in sys.argv:
            return sys.argv[sys.argv.index(argv_key)+1]

    def send_email(self,subject, body):
        if 'email' in sys.argv:
            return Email(self.log).sendemail(subject, body)
        else:
            return

    def import_module(self,pack):
        return PyPlugin.dyclass(pack)(self).run()